﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}

<script>
    var _State = "", OrganizeId = "", RoomId = "";
    $(function () {
        InitialPage();
        GetGrid();
        GetTree();
        // GetTree1();
    })

    function GetTree() {
        var item = {
            height: $(window).height() - 55,
            url: "../../QualificationManage/RentContract/GetTreeListTojson?OrganizeId=" + OrganizeId,
            onnodeclick: function (item) {
                switch (item.id) {
                    case "All":
                        _State = -1;
                        break;
                    case "UnSign":
                        _State = 1;
                        break;
                    case "UnHandled":
                        _State = 2;
                        break;
                    case "Current":
                        _State = 3;
                        break;
                    case "History":
                        _State = 4;
                        break;
                    case "Complete":
                        _State = 5;
                        break;
                    case "Days60":
                        _State = 31;
                        break;
                    case "WaitingCancel":
                        _State = 32;
                        break;
                    case "Changing":
                        _State = 33;
                        break;
                    case "Renewing":
                        _State = 34;
                        break;
                    case "Leaseing":
                        _State = 35;
                        break;
                    case "ExRenew":
                        _State = 41;
                        break;
                    case "ExLeaseing":
                        _State = 42;
                        break;
                    case "BeforeLease":
                        _State = 43;
                        break;
                    case "LaterLease":
                        _State = 44;
                        break;
                    case "ExChanging":
                        _State = 45;
                        break;
                    default: break;


                }
                SearchEvent();
            }
        };
        //初始化
        $("#itemTree1").treeview(item);
    }
    //function GetTree1()
    //{
    //    var item = {
    //        height: $(window).height() - 387,
    //        url: "../../ResourceManage/RS_PStructs/GetTreeJson?IsSubstep=true&OrganizeId=" + OrganizeId,
    //       // url: "../../QualificationManage/RentContract/GetRoomTreeJson?OrganizeId=" + OrganizeId,
    //        onnodeclick: function (item) {

    //            RoomId = item.id;
    //            if (item.id == "All") {
    //                RoomId = "";
    //            }
    //            $('#btn_Search').trigger("click");
    //        }
    //    }
    //    $("#itemTree2").treeview(item);
    //}
    function InitialPage() {
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 181.5);

            }, 200);
            e.stopPropagation();

        });
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            height: "250px",
            description: "===选择组织机构===",
            click: function (item) {
                OrganizeId = item.id;

                GetTree1();
                SearchEvent();
            }
        });
        //初始化时间

        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day+1).ToString("yyyy-MM-dd")");
        $("#EndTime").val("@DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")");
        $("#time_horizon").ComboBox({
            data: [{ "key": 2, "value": "上周" }, { "key": 1, "value": "本周" }, { "key": 4, "value": "上月" }, { "key": 3, "value": "本月" }, { "key": 5, "value": "本年" }],
            id: "key",
            text: "value",
           description: "=选择=",
            click: function (item) {

                var value = item.attr('data-value');
                switch (value) {
                    case "1"://本周
                        $("#StartTime").val("@DateTime.Now.AddDays(1 - DateTime.Now.DayOfWeek.GetHashCode()).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(7 - DateTime.Now.DayOfWeek.GetHashCode()).ToString("yyyy-MM-dd")");
                        break;
                    case "2"://上周
                        $("#StartTime").val("@DateTime.Now.AddDays(1 - DateTime.Now.DayOfWeek.GetHashCode()-7).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(7 - DateTime.Now.DayOfWeek.GetHashCode()-7).ToString("yyyy-MM-dd")");
                        break;
                    case "3"://本月
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day+1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    case "4"://上月
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day + 1).AddMonths(-1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    case "5"://本年
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.DayOfYear + 1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddMonths(12 - DateTime.Now.Month + 1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    default:
                        break;
                }
                $("#SelectedStartTime").html($("#StartTime").val());
                $("#SelectedEndTime").html($("#EndTime").val());
                SearchEvent();
            }
        });
        $("#time_horizon").ComboBoxSetValue(3);
    }
    function GetGrid() {
        var selectedRowIndex = 0;
        $gridTable = $("#gridTable");
        var queryJson = {
            StartTime: $("#StartTime").val(),
            EndTime: $("#EndTime").val()+" 23:59:59",
            Name: $("#Name").val(),
            Code: $("#Code").val(),
            IDCard: $("#IDCard").val(),
            State: _State,
            OrganizeId: OrganizeId,
            RoomId: RoomId,
            SourceType: ""
        };
        $gridTable.jqGrid({
            url: "../../QualificationManage/RentContract/GetPageList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: $(window).height() - 141,
            autowidth: true,
           // multiselect: true,
            colModel: [
                { label: "主键", name: "id", hidden: true },
                { label: "入住人", name: "name", index: "name", width: 100, align: "center", sortable: false },
                { label: "手机号", name: "phone", index: "phone", width: 150, align: "center", sortable: false },
                { label: "身份证号", name: "idcard", index: "idcard", width: 200, align: "center", sortable: false }, 
                { label: "所属单位", name: "companyname", index: "companyname", width: 200, align: "center", sortable: false },
                { label: "合同编号", name: "code", index: "code", width: 200, align: "center", sortable: false },
                { label: "租赁房产", name: "allname", index: "allname", width: 200, align: "center", sortable: false },
                { label: "状态", name: "state", hidden: true },
                {
                    label: "签约状态", name: "signdatestr", index: "signdate", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        var msg = cellvalue;
                        if (rowObject.state == 1 || rowObject.state == "1") {
                            msg = "待签约";
                        }
                        if (rowObject.state == 2 || rowObject.state == "2") {
                            msg = "待执行";
                        }
                        if (rowObject.state == 3 || rowObject.state == "3") {
                            msg = "当前";
                        }
                        if (rowObject.state == 4 || rowObject.state == "4") {
                            msg = "历史";
                        }
                        if (rowObject.state == 5 || rowObject.state == "5") {
                            msg = "结束";
                        }
                        return msg;

                    }
                },
                 {
                     label: "签约主体", name: "signtype", index: "signtype", width: 100, align: "center", sortable: false,
                     formatter: function (cellvalue) {
                         var Msg = "";
                         switch (cellvalue) {
                             case 0:
                             case "0":
                             case "1":
                             case 1: Msg = "单位";
                                 break;
                             case "2":
                             case 2: Msg = "个人";
                                 break;
                             default: Msg = cellvalue;
                                 break;
                         }
                         return Msg;
                     }
                 },
                {
                    label: "签约时间", name: "signdate", index: "signdate", width: 150, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        var msg = cellvalue;
                        if (rowObject.state == 1 || rowObject.state == "1") {
                            msg = "";
                        }
                        if (cellvalue == null && rowObject.state != 1) {
                            msg = "";
                        }
                        return msg;

                    }
                },
                { label: "市局编号", name: "cityburceaucode", index: "cityburceaucode", width: 150, align: "center", sortable: false },
                
               
                { label: "入住人ID", name: "liveinuserid", hidden: true },
                
                {
                    label: "起始日期", name: "begindate", index: "begindate", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue) {
                        return formatDate(cellvalue, 'yyyy-MM-dd');
                    }

                },
                {
                    label: "终止日期", name: "enddate", index: "enddate", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue) {
                        return formatDate(cellvalue, 'yyyy-MM-dd');
                    }
                },
                { label: "月租金", name: "monthlyrent", index: "monthlyrent", width: 100, align: "center", sortable: false },
                { label: "押金", name: "deposit", index: "deposit", width: 100, align: "center", sortable: false },
                { label: "备注", name: "description", index: "description", width: 200, align: "center", sortable: false }
            ],
            viewrecords: true,
            rowNum: 30,
            rowList: [30, 50, 100],
            pager: "#gridPager",
            sortable: true,
            sortname: 'Code',
            sortorder: 'desc',
            rownumbers: true,
            gridview: true,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
                var State = $gridTable.getCell(status, "state");
                //if (State > 1) {
                //    $gridTable.jqGrid("setSelection", status, false);
                //    return false;
                //}
            },
            gridComplete: function () {
                // $("#" + this.id).setSelection(selectedRowIndex, false);
                var rowIds = $gridTable.jqGrid('getDataIDs');
                for (var k = 0; k < rowIds.length; k++) {
                    var curRowData = $gridTable.jqGrid('getRowData', rowIds[k]);
                    var curChk = $("#" + rowIds[k] + "").find(":checkbox");
                    curChk.attr('name', 'checkboxname');   //给每一个checkbox赋名字
                    curChk.attr('value', curRowData['MY_ID']);   //给checkbox赋值
                    curChk.attr('title', curRowData['NAME']);
                }
            }
        });
        //查询事件
        $("#btn_Search").click(function () {
            SearchEvent();
            $('#btn_Reset').trigger("click");
            $(".ui-filter-text").trigger("click");
        });
        $("#btn_Reset").click(function () { 
            $("#Name").val("");
            $("#Code").val("");
            $("#IDCard").val("");
            $("#PStruct").val("");

        })

    }
    function SearchEvent()
    {
        var queryJson = {
            StartTime: $("#StartTime").val(),
            EndTime: $("#EndTime").val() + " 23:59:59",
            Name: $("#Name").val(),
            Code: $("#Code").val(),
            IDCard: $("#IDCard").val(),
            PStruct: $("#PStruct").val(),
            State: _State,
            OrganizeId: OrganizeId,
            RoomId: RoomId,
            SourceType: 1
        };
        $("#gridTable").jqGrid('setGridParam', {
            url: "../../QualificationManage/RentContract/GetPageList",
            postData: { queryJson: JSON.stringify(queryJson) },
        }).trigger('reloadGrid');
    }

    function btn_export() {
        var queryJson = {
            StartTime: $("#StartTime").val(),
            EndTime: $("#EndTime").val() + " 23:59:59",
            Name: $("#Name").val(),
            Code: $("#Code").val(),
            IDCard: $("#IDCard").val(),
            PStruct: $("#PStruct").val(),
            State: _State,
            OrganizeId: OrganizeId,
            RoomId: RoomId,
            SourceType: 1
        };
        location.href = "../../QualificationManage/RentContract/GetExportList?queryJson=" + JSON.stringify(queryJson);
    }

    function btn_detail() {
        var keyValue = $("#gridTable").jqGridRowValue("id");
        var Code = $("#gridTable").jqGridRowValue("code");
        if (checkedRow(keyValue)) {
            top.tablist.newTab({
                id: "Form",
                title: '合同详情【' + Code + '】',
                closed: true,
                icon: "fa fa-eye",
                url: top.contentPath + '/QualificationManage/RentContract/Form?keyValue=' + keyValue
            });
        }

    }
    //批量启用
    function btn_AllEnabled() {
        var postData = [];
        var $gridTable = $('#gridTable');
        var obj = $gridTable.jqGrid("getRowData");
        for (var i = 0; i < obj.length; i++) {
            var Ischk = $("#jqg_gridTable_" + obj[i].id).is(':checked');
            var IsEnabled = obj[i].state;
            if (Ischk && IsEnabled == 2) { //过滤合同状态，只保留选中的待执行合同
                postData.push(obj[i].id);
            }
        }
        if (postData.length <= 0) {
            top.dialogTop('至少选择一条有效数据', 'error');
            return false;
        }
        dialogConfirm('该操作只针对合同状态为待执行的合同,<br/>执行操作后将无法逆转,是否继续！', function (r) {
            if (r) {
                $.SaveForm({
                    url: "../../QualificationManage/RentContract/AllEdit",
                    param: { Type: 1, postData: JSON.stringify(postData) },
                    loading: "正在保存数据...",
                    success: function () {
                        $('#btn_Search').trigger("click");
                    }
                })
            }
        });
    }
    //批量取消
    function btn_AllClose() {
        var postData = [];
        var $gridTable = $('#gridTable');
        var obj = $gridTable.jqGrid("getRowData");
        for (var i = 0; i < obj.length; i++) {
            var Ischk = $("#jqg_gridTable_" + obj[i].id).is(':checked');
            var IsEnabled = obj[i].state;
            if (Ischk && IsEnabled == 1) { //过滤合同状态，只保留选中的待签约合同
                postData.push(obj[i].id);
            }
        }
        if (postData.length <= 0) {
            top.dialogTop('至少选择一条有效数据', 'error');
            return false;
        }
        dialogConfirm('该操作只针对合同状态为未签约的合同，<br/>执行操作后将无法逆转,是否继续！', function (r) {
            if (r) {
                $.SaveForm({
                    url: "../../QualificationManage/RentContract/AllEdit",
                    param: { Type: 2, postData: JSON.stringify(postData) },
                    loading: "正在保存数据...",
                    success: function () {
                        $('#btn_Search').trigger("click");
                    }
                })

            }
        });

    }

</script>
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-west">
        <div class="west-Panel">
            @*<div class="panel-Title" style="padding:0px;"><div id="OrganizeId" type="selecttree" class="ui-select "></div></div>*@
            <div class="panel-Title" style="padding:0px; text-align:center;">合同状态</div>
            <div id="itemTree1"></div>
        </div>
    </div>
    <div class="ui-layout-center">
        <div class="center-Panel">
            <div class="titlePanel">
                <div class="title-search ">
                    <table>
                        <tr>
                            <td>签约日期</td>
                            <td style="padding-left: 10px;">
                                <div class="ui-filter" style="width: 200px;">
                                    <div class="ui-filter-text">
                                        <strong id="SelectedStartTime">@DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToString("yyyy-MM-dd")</strong> 至 <strong id="SelectedEndTime">@DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")</strong>
                                    </div>
                                    <div class="ui-filter-list" style="width: 350px;">
                                        <table class="form" id="filter-form">
                                            <tr>
                                                <th class="formTitle">签约日期：</th>
                                                <td class="formValue">
                                                    <div style="float: left; width: 45%;">
                                                        <input id="StartTime" readonly type="text" value="" class="form-control input-wdatepicker" onfocus="WdatePicker()">
                                                    </div>
                                                    <div style="float: left; width: 10%; text-align: center;">至</div>
                                                    <div style="float: left; width: 45%;">
                                                        <input id="EndTime" readonly type="text" value="@DateTime.Now.AddDays(-DateTime.Now.Day).AddMonths(1).ToString("yyyy-MM-dd")" class="form-control input-wdatepicker" onfocus="WdatePicker()">
                                                    </div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle">维修范围</td>
                                                <td class="formValue">
                                                    <div id="Range" type="selectTree" class="ui-select">

                                                    </div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle">姓名：</td>
                                                <td class="formValue">
                                                    <input id="Name" maxlength="20" type="text" class="form-control">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle">合同编号：</td>
                                                <td class="formValue">
                                                    <input id="Code" maxlength="20" type="text" class="form-control">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle">身份证号：</td>
                                                <td class="formValue">
                                                    <input id="IDCard" maxlength="20" type="text" class="form-control">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle">租赁房产：</td>
                                                <td class="formValue">
                                                    <input id="PStruct" maxlength="20" type="text" class="form-control">
                                                </td>
                                            </tr>
                                        </table>
                                        <div class="ui-filter-list-bottom">
                                            <a id="btn_Reset" class="btn btn-default">&nbsp;重&nbsp;&nbsp;置</a>
                                            <a id="btn_Search" class="btn btn-primary">&nbsp;查&nbsp;&nbsp;询</a>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td style="padding-left: 10px;">
                                <div id="time_horizon" type="selectTree" class="ui-select" style="width: 60px;"></div>
                            </td>


                        </tr>

                    </table>

                </div>

                <div class="toolbar">
                    <div class="btn-group">
                        <a id="lr-replace" class="btn btn-default " onclick="reload(); "><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                        <a id="lr-add" class="btn btn-default " onclick="btn_detail()"><i class="fa fa-eye"></i>&nbsp;查看</a>
                        <a id="lr-export" class="btn btn-default" onclick="btn_export()"><i class="fa fa-sign-out"></i>&nbsp;导出</a>
                        @*<a id="lr-AllEnabled" class="btn btn-default " onclick="btn_AllEnabled()"><i class="fa fa-pencil-square-o"></i>&nbsp;启用</a>
                        <a id="lr-AllClose" class="btn btn-default " onclick="btn_AllClose()"><i class="fa fa-trash-o"></i>&nbsp;取消</a>*@
                        
                    </div>
                     <script>$('.toolbar').authorizeButton()</script> 
                </div>
            </div>


            <div class="gridPanel">
                <table id="gridTable"></table>
                <div id="gridPager"></div>
            </div>

        </div>
    </div>
</div>
